//
//  XQBaseTableListArrowView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/21.
//

#import "XQBaseTableListArrowView.h"
#import "XQBaseTableListArrowCell.h"

@interface XQBaseTableListArrowView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@end

@implementation XQBaseTableListArrowView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.tableView];
}

- (void)makeConstraints {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(0);
        make.bottom.equalTo(@0);
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.row];
    XQBaseTableListArrowCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XQBaseTableListArrowCell" forIndexPath:indexPath];
    [cell setTitle:dic[@"won"]];
    [cell setBottomPadding:indexPath.row == self.dataSource.count - 1 ? 0 : 29];
    @weakify(self)
    cell.btnHandle = ^{
        @strongify(self);
        [self cellDidClickAtIndexPath:indexPath];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.row == self.dataSource.count - 1 ? 23 : 52;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self cellDidClickAtIndexPath:indexPath];
}

- (void)cellDidClickAtIndexPath:(NSIndexPath *)indexPath {
    if (self.cellClickHandle) {
        
        NSDictionary *dic = self.dataSource[indexPath.row];
        if (dic[@"aviation"]) {
            self.cellClickHandle(dic[@"aviation"]);
        }else {
            self.cellClickHandle(dic[@"won"]);
        }
    }
}

#pragma mark - setter

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    [self.tableView reloadData];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40 + 23 * self.dataSource.count + 29 * (self.dataSource.count - 1));
    }];
}


#pragma mark - lazy

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.layer.cornerRadius = 15;
        [_tableView setContentInset:UIEdgeInsetsMake(20, 0, 20, 0)];
        [_tableView setScrollEnabled:NO];
        [_tableView registerClass:[XQBaseTableListArrowCell class] forCellReuseIdentifier:@"XQBaseTableListArrowCell"];
    }
    return _tableView;
}

@end
